<template>
	<view class="container">
		<view class="boxPanel rel" v-for="(item,index) in pdfList" :key="index">
			<view class="flex-between m-b-20">
				<view class="c-77">名称</view>
				<view class="">检测报告{{index+1}}</view>
			</view>
			<view class="flex-between">
				<view class="c-77">上传时间</view>
				<view class="">{{$util.timeStampTurnTime(item.create_time)}}</view>
			</view>
			<view class="make" @click="previewPdf(item)">报告详情</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				id: '',
				pdfList: [],

			}
		},
		onLoad(data) {
			this.isIphoneX = this.$util.uniappIsIPhoneX()
			this.id = data.id;
			this.getData();
		},
		methods: {
			getData() {
				this.$api.sendRequest({
			      url: "/api/userHealth/getPdf",
			      data: {
					hId: this.id
				  },
			      success: res => {
			        if(res.code == 0) {
						this.pdfList = res.data;
			        }else {
						this.$util.showToast({title: res.message});
					}
			      }
			    })
			},
			previewPdf(item) {
				// 检查是否有PDF文件路径
				if(!item.pdf_path) {
					this.$util.showToast({title: 'PDF文件不存在'});
					return;
				}
				
				// 使用uniapp的API打开PDF文件
				uni.downloadFile({
					url: this.$config.imgDomain + item.pdf_path,
					success: (res) => {
						if(res.statusCode === 200) {
							const filePath = res.tempFilePath;
							uni.openDocument({
								filePath: filePath,
								fileType: 'pdf',
								success: function() {
									console.log('打开PDF成功');
								},
								fail: function(err) {
									console.log('打开PDF失败', err);
									this.$util.showToast({title: '打开PDF失败'});
								}
							});
						}
					},
					fail: (err) => {
						console.log('下载PDF失败', err);
						this.$util.showToast({title: '下载PDF失败'});
					}
				});
			},
		}
	}
</script>

<style lang="scss" scoped>
.make {
	width: 139rpx;
	height: 52rpx;
	line-height: 52rpx;
	text-align: center;
	background-color: $color-title2;
	color: #fff;
	font-size: 24rpx;
	border-radius: 60rpx;
	margin-top: 10rpx;
	margin-left: auto;
}
</style>
